from flask import Blueprint, request, jsonify
from controllers.common import *
from controllers.db import *
from controllers.config import *
import requests

view_reward_replace_result_api = Blueprint('view_reward_replace_result_api', __name__)

@view_reward_replace_result_api.route("/view_reward_replace_result", methods=['POST'])
def project_action():
    result = {'msg': '成功', 'code': 200, 'count':0, 'ret': 0, 'from_loc': '', 'to_loc': ''}
    update_data = json.loads(request.data)
    event_type = update_data['event_type']
    _type = update_data['type']

    if event_type == 'continuous_activities_proj':
        # task_group task_chest_reward top_reward
        from_type = update_data['from_type']
        from_id = update_data['from_id']
        to_type = update_data['to_type']
        to_id = update_data['to_id']
        project = update_data['project']

        query = ContinuousActivitiesProj.query.filter_by(project=project).first()
        task_group = json.loads(query.task_group)
        task_chest_reward = json.loads(query.task_chest_reward)
        top_reward = json.loads(query.top_reward)

        if _type == 'view':
            reward_list = {}
            req = requests.post(url=f"{RESOURCE_SERVICE_URL}/get_reward", json={
                'project': PROJECT_NAME,
                'environment': EVN,
                'part': ['reward_list']
            })
            if req.status_code == 200:
                resp = req.json()
                if resp['status'] == 'success':
                    reward_list = resp['reward_list']
            if not reward_list or str(from_type) not in reward_list or str(to_type) not in reward_list:
                result['ret'] == 1
                result['msg'] == '未找到以上奖励类型'
                return jsonify(result)

            from_array = reward_list[str(from_type)]
            for node in from_array:
                if node['id'] == from_id:
                    result['from_loc'] = node['value']
                    break

            to_array = reward_list[str(to_type)]
            for node in to_array:
                if node['id'] == to_id:
                    result['to_loc'] = node['value']
                    break

            for node in task_group:
                for t_node in node['task']:
                    for item_node in t_node['task_item']:
                        for r_node in item_node['reward']:
                            if r_node['a'][0] == from_type and r_node['a'][1] == from_id:
                                result['count'] += 1

            for node in task_chest_reward:
                if node['a'][0] == from_type and node['a'][1] == from_id:
                    result['count'] += 1

            for node in top_reward:
                if node['a'][0] == from_type and node['a'][1] == from_id:
                    result['count'] += 1

        elif _type == 'replace':
            for node in task_group:
                for t_node in node['task']:
                    for item_node in t_node['task_item']:
                        for r_node in item_node['reward']:
                            if r_node['a'][0] == from_type and r_node['a'][1] == from_id:
                                r_node['a'][0], r_node['a'][1] = to_type, to_id

            for node in task_chest_reward:
                if node['a'][0] == from_type and node['a'][1] == from_id:
                    node['a'][0], node['a'][1] = to_type, to_id

            for node in top_reward:
                if node['a'][0] == from_type and node['a'][1] == from_id:
                    node['a'][0], node['a'][1] = to_type, to_id

            query.task_group = json.dumps(task_group)
            query.task_chest_reward = json.dumps(task_chest_reward)
            query.top_reward = json.dumps(top_reward)
            db.session.commit()

    elif event_type == 'target_activity_proj':
        # task_group basic
        from_type = update_data['from_type']
        from_id = update_data['from_id']
        to_type = update_data['to_type']
        to_id = update_data['to_id']
        project = update_data['project']

        reward_list = {}
        req = requests.post(url=f"{RESOURCE_SERVICE_URL}/get_reward", json={
            'project': PROJECT_NAME,
            'environment': EVN,
            'part': ['reward_list']
        })
        if req.status_code == 200:
            resp = req.json()
            if resp['status'] == 'success':
                reward_list = resp['reward_list']
        if not reward_list or str(from_type) not in reward_list or str(to_type) not in reward_list:
            result['ret'] == 1
            result['msg'] == '未找到以上奖励类型'
            return jsonify(result)

        from_array = reward_list[str(from_type)]
        for node in from_array:
            if node['id'] == from_id:
                result['from_loc'] = node['value']
                break

        to_array = reward_list[str(to_type)]
        for node in to_array:
            if node['id'] == to_id:
                result['to_loc'] = node['value']
                break

        query = TargetActivityProj.query.filter_by(project=project).first()
        task_group = json.loads(query.task_group)
        basic = json.loads(query.basic)

        if _type == 'view':
            for node in task_group:
                for t_node in node:
                    for r_node in t_node['reward']:
                        if r_node['type'] == from_type and r_node['id'] == from_id:
                            result['count'] += 1

            for node in basic['calendar_reward']:
                if node['type'] == from_type and node['id'] == from_id:
                    result['count'] += 1

        elif _type == 'replace':
            for node in task_group:
                for t_node in node:
                    for r_node in t_node['reward']:
                        if r_node['type'] == from_type and r_node['id'] == from_id:
                            r_node['type'], r_node['id'], r_node['name'] = to_type, to_id, result['to_loc']

            for node in basic['calendar_reward']:
                if node['type'] == from_type and node['id'] == from_id:
                    node['type'], node['id'], node['name'] = to_type, to_id, result['to_loc']

            query.task_group = json.dumps(task_group)
            query.basic = json.dumps(basic)
            db.session.commit()

    elif event_type == 'make_cake_event':
        # task_group basic
        from_type = update_data['from_type']
        from_id = update_data['from_id']
        to_type = update_data['to_type']
        to_id = update_data['to_id']
        project = update_data['project']

        reward_list = {}
        req = requests.post(url=f"{RESOURCE_SERVICE_URL}/get_reward", json={
            'project': PROJECT_NAME,
            'environment': EVN,
            'part': ['reward_list']
        })
        if req.status_code == 200:
            resp = req.json()
            if resp['status'] == 'success':
                reward_list = resp['reward_list']
        if not reward_list or str(from_type) not in reward_list or str(to_type) not in reward_list:
            result['ret'] == 1
            result['msg'] == '未找到以上奖励类型'
            return jsonify(result)

        from_array = reward_list[str(from_type)]
        for node in from_array:
            if node['id'] == from_id:
                result['from_loc'] = node['value']
                break

        to_array = reward_list[str(to_type)]
        for node in to_array:
            if node['id'] == to_id:
                result['to_loc'] = node['value']
                break

        query = MakeCakeEvent.query.filter_by(project=project).first()
        goal_reward = json.loads(query.goal_reward)
        extern0 = json.loads(query.extern0)

        if _type == 'view':
            for node in goal_reward:
                for t_node in node['reward']:
                    for r_node in t_node:
                        if r_node['a'][0] == from_type and r_node['a'][1] == from_id:
                            result['count'] += 1

            for node in extern0['lv_up']:
                if node['cost']['a'][0] == from_type and node['cost']['a'][1] == from_id:
                    result['count'] += 1

            for node in extern0['extra_reward']['reward']:
                for r_node in node['reward']:
                    if r_node['a'][0] == from_type and r_node['a'][1] == from_id:
                        result['count'] += 1

        elif _type == 'replace':
            for node in goal_reward:
                for t_node in node['reward']:
                    for r_node in t_node:
                        if r_node['a'][0] == from_type and r_node['a'][1] == from_id:
                            r_node['a'][0], r_node['a'][1] = to_type, to_id

            for node in extern0['lv_up']:
                if node['cost']['a'][0] == from_type and node['cost']['a'][1] == from_id:
                    node['cost']['a'][0], node['cost']['a'][1] = to_type, to_id

            for node in extern0['extra_reward']['reward']:
                for r_node in node['reward']:
                    if r_node['a'][0] == from_type and r_node['a'][1] == from_id:
                        r_node['a'][0], r_node['a'][1] = to_type, to_id

            query.goal_reward = json.dumps(goal_reward)
            query.extern0 = json.dumps(extern0)
            db.session.commit()

    elif event_type == 'new_personal_event_project_v2':
        # task_group basic
        from_type = update_data['from_type']
        from_id = update_data['from_id']
        to_type = update_data['to_type']
        to_id = update_data['to_id']
        project = update_data['project']

        reward_list = {}
        req = requests.post(url=f"{RESOURCE_SERVICE_URL}/get_reward", json={
            'project': PROJECT_NAME,
            'environment': EVN,
            'part': ['reward_list']
        })
        if req.status_code == 200:
            resp = req.json()
            if resp['status'] == 'success':
                reward_list = resp['reward_list']
        if not reward_list or str(from_type) not in reward_list or str(to_type) not in reward_list:
            result['ret'] == 1
            result['msg'] == '未找到以上奖励类型'
            return jsonify(result)

        from_array = reward_list[str(from_type)]
        for node in from_array:
            if node['id'] == from_id:
                result['from_loc'] = node['value']
                break

        to_array = reward_list[str(to_type)]
        for node in to_array:
            if node['id'] == to_id:
                result['to_loc'] = node['value']
                break

        query = NewPersonalEventProjectV2.query.filter_by(project=project).first()
        reward = json.loads(query.reward)
        basic = json.loads(query.basic)

        if _type == 'view':
            for node in reward['goal_reward']:
                for r_node in node['reward']:
                    if r_node['type'] == from_type and r_node['id'] == from_id:
                        result['count'] += 1

            for node in reward['rank_reward']:
                for r_node in node['reward']:
                    if r_node['type'] == from_type and r_node['id'] == from_id:
                        result['count'] += 1

            for node in basic['calendar_reward']:
                if node['type'] == from_type and node['id'] == from_id:
                    result['count'] += 1

        elif _type == 'replace':
            for node in reward['goal_reward']:
                for r_node in node['reward']:
                    if r_node['type'] == from_type and r_node['id'] == from_id:
                        r_node['type'], r_node['id'], r_node['name'] = to_type, to_id, result['to_loc']

            for node in reward['rank_reward']:
                for r_node in node['reward']:
                    if r_node['type'] == from_type and r_node['id'] == from_id:
                        r_node['type'], r_node['id'], r_node['name'] = to_type, to_id, result['to_loc']

            for node in basic['calendar_reward']:
                if node['type'] == from_type and node['id'] == from_id:
                    node['type'], node['id'], node['name'] = to_type, to_id, result['to_loc']

            query.reward = json.dumps(reward)
            query.basic = json.dumps(basic)
            db.session.commit()

    elif event_type == 'strongest_sheriff_project_v2':
        # task_group basic
        from_type = update_data['from_type']
        from_id = update_data['from_id']
        to_type = update_data['to_type']
        to_id = update_data['to_id']
        project = update_data['project']

        reward_list = {}
        req = requests.post(url=f"{RESOURCE_SERVICE_URL}/get_reward", json={
            'project': PROJECT_NAME,
            'environment': EVN,
            'part': ['reward_list']
        })
        if req.status_code == 200:
            resp = req.json()
            if resp['status'] == 'success':
                reward_list = resp['reward_list']
        if not reward_list or str(from_type) not in reward_list or str(to_type) not in reward_list:
            result['ret'] == 1
            result['msg'] == '未找到以上奖励类型'
            return jsonify(result)

        from_array = reward_list[str(from_type)]
        for node in from_array:
            if node['id'] == from_id:
                result['from_loc'] = node['value']
                break

        to_array = reward_list[str(to_type)]
        for node in to_array:
            if node['id'] == to_id:
                result['to_loc'] = node['value']
                break

        query = StrongestSheriffProjectV2.query.filter_by(project=project).first()
        reward = json.loads(query.reward)
        basic = json.loads(query.basic)
        sub = json.loads(query.sub)

        if _type == 'view':
            for node in reward['rank_reward']:
                for r_node in node['reward']:
                    if r_node['type'] == from_type and r_node['id'] == from_id:
                        result['count'] += 1

            for node in reward['sub_rank_reward']:
                for r_node in node['reward']:
                    if r_node['type'] == from_type and r_node['id'] == from_id:
                        result['count'] += 1

            for node in basic['calendar_reward']:
                if node['type'] == from_type and node['id'] == from_id:
                    result['count'] += 1

            for sub_pid in sub:
                sub_query = StrongestSheriffProjectV2Sub.query.filter_by(project=sub_pid).first()
                sub_reward = json.loads(sub_query.reward)
                for node in sub_reward['goal_reward']:
                    for r_node in node['reward']:
                        if r_node['type'] == from_type and r_node['id'] == from_id:
                            result['count'] += 1

        elif _type == 'replace':
            for node in reward['rank_reward']:
                for r_node in node['reward']:
                    if r_node['type'] == from_type and r_node['id'] == from_id:
                        r_node['type'], r_node['id'], r_node['name'] = to_type, to_id, result['to_loc']

            for node in reward['sub_rank_reward']:
                for r_node in node['reward']:
                    if r_node['type'] == from_type and r_node['id'] == from_id:
                        r_node['type'], r_node['id'], r_node['name'] = to_type, to_id, result['to_loc']

            for node in basic['calendar_reward']:
                if node['type'] == from_type and node['id'] == from_id:
                    node['type'], node['id'], node['name'] = to_type, to_id, result['to_loc']

            for sub_pid in sub:
                sub_query = StrongestSheriffProjectV2Sub.query.filter_by(project=sub_pid).first()
                sub_reward = json.loads(sub_query.reward)
                for node in sub_reward['goal_reward']:
                    for r_node in node['reward']:
                        if r_node['type'] == from_type and r_node['id'] == from_id:
                            r_node['type'], r_node['id'], r_node['name'] = to_type, to_id, result['to_loc']
                sub_query.reward = json.dumps(sub_reward)

            query.reward = json.dumps(reward)
            query.basic = json.dumps(basic)
            db.session.commit()

    return jsonify(result)

